//
//  LGNormalClinicController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2024/8/2.
//  房颤 -- 常规门诊、慢病复查

#import "LGNormalClinicController.h"
//患者档案
#import "LGPatientDetailController_YHJK.h"
#import "LGPatientDetailController2.h"
//用药管理
#import "LGMedicineManageController.h"
//体征监测
#import "LGHomeMonitoringController.h"
//报告中心
#import "LGReportingCenterController.h"
//随访计划
#import "LGPatientCarePlanController.h"
//开具医嘱
#import "LGAddDoctorAdviceController.h"
//随访总结
#import "LGMedicineBallFollowUpRecorder.h"
//风险检测
#import "LGPatientRiskMonitorController.h"
#import "LGGetWebViewController.h"

#import "LGNormalClinicHeader.h"
#import "LGBasicInfoCommonCell.h"
#import "LGBaseInfoHeaderView.h"
#define viewHeight  (Screen_H-topBarHeight-viewPix(44))

@interface LGNormalClinicController ()<UITableViewDelegate,UITableViewDataSource,LGBaseInfoHeaderViewDelegate,LGBottomButtonDelegate,LGNormalClinicHeaderDelegate>
/**   */
@property (nonatomic , strong)LGNormalClinicHeader *headView;
/**   */
@property (nonatomic , strong)LGBottomButtonView *bottomView;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)NSDictionary *dataDic;
/**   */
@property (nonatomic , strong)NSArray *dataArray;
/**   */
@property (nonatomic , assign)BOOL canEdit;

@end

@implementation LGNormalClinicController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithString:@"#F3F7F8"];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.bottomView];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestData) name:@"RefreshSurgeryData" object:nil];
    
    //风险提示
    NSDictionary *riskDic = @{@"title":@"风险提示",@"key":@"risk",@"editKey":@"edit_url",@"timeKey":@"update_time",@"items":@[
    @{@"title":@"中风风险",@"key":@"zhongf",@"superKey":@"risk"},
    @{@"title":@"出血风险",@"key":@"chux",@"superKey":@"risk"},
    @{@"title":@"其他",@"key":@"other",@"superKey":@"risk"}
    ]};
    
    //诊断
    NSDictionary *diagnoseDic = @{@"title":@"诊断",@"key":@"disease",@"editKey":@"edit_url",@"timeKey":@"update_time",@"items":@[
    @{@"title":@"心律失常类型",@"key":@"xlsclx",@"superKey":@"disease"},
    @{@"title":@"您合并疾病",@"key":@"hebingz_fc",@"superKey":@"disease"},
    @{@"title":@"症状",@"key":@"zhengz_fc",@"superKey":@"disease"},
    @{@"title":@"目前药物",@"key":@"yaowu",@"superKey":@"disease"},
    ]};
    
    //健康史
    NSDictionary *healthDic = @{@"title":@"健康史",@"key":@"jbxx",@"editKey":@"edit_url",@"timeKey":@"update_time",@"items":@[
    @{@"title":@"既往病史",@"key":@"jibings_fa",@"superKey":@"jbxx"},
    @{@"title":@"手术史",@"key":@"shouss_fc",@"superKey":@"jbxx"},
    @{@"title":@"家族病史",@"key":@"jiazushi_fc",@"superKey":@"jbxx"},
    @{@"title":@"过敏史",@"key":@"guomings",@"superKey":@"jbxx"},
    @{@"title":@"出血史",@"key":@"chuxue",@"superKey":@"jbxx"},
    @{@"title":@"吸烟情况",@"key":@"smoking",@"superKey":@"jbxx"},
    @{@"title":@"饮酒情况",@"key":@"alcohol",@"superKey":@"jbxx"}
    ]};
    
    if ([[NSObject appType:self.diseaseCode] isEqualToString:@"HX"]) {
        //心康管家没有出血史
        healthDic = @{@"title":@"健康史",@"key":@"jbxx",@"editKey":@"edit_url",@"timeKey":@"update_time",@"items":@[
        @{@"title":@"既往病史",@"key":@"jibings_fa",@"superKey":@"jbxx"},
        @{@"title":@"手术史",@"key":@"shouss_fc",@"superKey":@"jbxx"},
        @{@"title":@"家族病史",@"key":@"jiazushi",@"superKey":@"jbxx"},
        @{@"title":@"过敏史",@"key":@"guomings",@"superKey":@"jbxx"},
        @{@"title":@"吸烟情况",@"key":@"smoking",@"superKey":@"jbxx"},
        @{@"title":@"饮酒情况",@"key":@"alcohol",@"superKey":@"jbxx"}
        ]};
    }
    
    //体征信息
    NSDictionary *bodayDic = @{@"title":@"体征信息",@"key":@"bmi",@"editKey":@"bmi_edit_url",@"timeKey":@"bmi_update",@"items":@[
    @{@"title":@"身高(cm)",@"key":@"height",@"subKey":@"",@"status":@"",@"superKey":@"bmi"},
    @{@"title":@"体重(kg)",@"key":@"weight",@"subKey":@"",@"status":@"",@"superKey":@"bmi"},
    @{@"title":@"BMI",@"key":@"bmi",@"subKey":@"bmi_status_content",@"status":@"bmi_status",@"superKey":@"bmi",@"color":@{@"1":@"#E53C39",@"2":@"#FF9704",@"3":@"#4C87FD",@"4":@"#3DC997"}},
    @{@"title":@"腰围(cm)",@"key":@"waistline",@"subKey":@"waistline_status_content",@"status":@"waistline_status",@"superKey":@"bmi",@"color":@{@"1":@"#E53C39",@"2":@"#4C87FD",@"3":@"#FF9704"}},
    @{@"title":@"血压(mmHg)",@"key":@"bp",@"subKey":@"bp_status_content",@"status":@"bp_status",@"superKey":@"bmi",@"color":@{@"1":@"#C14D4D",@"2":@"#FF3535",@"3":@"#FE6603",@"4":@"#FEBB36",@"5":@"#4C87FD"}},
    @{@"title":@"安静心率(次/分钟)",@"key":@"hr",@"subKey":@"hr_status_content",@"status":@"hr_status",@"superKey":@"bmi",@"color":@{@"1":@"#E53C39",@"2":@"#4C87FD",@"3":@"#FF9704"}},
    ]};
    
    //基本信息
    NSDictionary *basicDic = @{@"title":@"基本信息",@"key":@"base",@"editKey":@"base_edit_url",@"timeKey":@"base_update",@"items":@[
    @{@"title":@"姓名",@"key":@"realname",@"superKey":@"base"},
    @{@"title":@"性别",@"key":@"gender",@"superKey":@"base"},
    @{@"title":@"出生日期",@"key":@"birthday",@"superKey":@"base"},
    @{@"title":@"年龄",@"key":@"age",@"superKey":@"base"},
    @{@"title":@"手机号",@"key":@"mobile",@"superKey":@"base"},
    @{@"title":@"就诊号",@"key":@"hospital_bano",@"superKey":@"base"},
    @{@"title":@"住院号",@"key":@"hospital_cardno",@"superKey":@"base"},
    @{@"title":@"最高学历",@"key":@"education",@"superKey":@"renkx"},
    @{@"title":@"婚姻状况",@"key":@"married",@"superKey":@"renkx"},
    @{@"title":@"职业情况",@"key":@"profession",@"superKey":@"renkx"},
    @{@"title":@"居住地",@"key":@"address",@"superKey":@"renkx"},
    @{@"title":@"紧急联系人电话",@"key":@"emergency_mobile",@"superKey":@"base"},
    ]};
    
    if ([[NSObject appType:self.diseaseCode] isEqualToString:@"HX"]) {
        self.dataArray = @[healthDic,bodayDic,basicDic];
        
    }else{
        self.dataArray = @[riskDic,diagnoseDic,healthDic,bodayDic,basicDic];
    }
    
    
    [self.tableView reloadData];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/patient.Profile/detailBaseFc" parameters:@{@"patient_id":self.patientid,@"disease_code":self.diseaseCode,@"visit_id":self.visit_id,@"followup_date":self.visit_date} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            self.canEdit = [LGNSString(returnData[@"is_edit"]) isEqualToString:@"1"]?YES:NO;
            self.headView.basicDic = returnData[@"base"];
            self.headView.visitDic = returnData[@"visit"];
            self.dataDic = returnData;
            [self.tableView reloadData];
        }
    } error:^(NSError *error) {
        
    }];
}

//头部
-(void)headViewButtonTouched:(NSString *)type{
    if ([type isEqualToString:@"detail"]) {
        //癌症-患者详情
        LGPatientDetailController_YHJK *controller = [[LGPatientDetailController_YHJK alloc]init];
        controller.patientid = self.patientid;
        controller.diseaseCode = self.diseaseCode;
        [self.navigationController pushViewController:controller animated:YES];
        

//        if ([self.diseaseCode isEqualToString:@"24"] || [self.diseaseCode isEqualToString:@"25"]) {
//            //市一慢阻肺
//            LGPatientDetailController2 *controller = [[LGPatientDetailController2 alloc]init];
//            controller.patientid = self.patientid;
//            controller.levelid = LGNSString(self.levelid);
//            controller.diseaseCode = self.diseaseCode;
//            [self.navigationController pushViewController:controller animated:YES];
//        }else{
//            //其他
//            LGPatientDetailController_YHJK *controller = [[LGPatientDetailController_YHJK alloc]init];
//            controller.patientid = self.patientid;
//            controller.levelid = LGNSString(self.levelid);
//            controller.diseaseCode = self.diseaseCode;
//            [self.navigationController pushViewController:controller animated:YES];
//        }
        
        
        
    }else if ([type isEqualToString:@"report"]){
        //报告中心
        LGReportingCenterController *controller = [[LGReportingCenterController alloc]init];
        controller.patientid = self.patientid;
        controller.diseaseCode = self.diseaseCode;
        controller.currentIndex = 2;
        [self.navigationController pushViewController:controller animated:YES];
        
    }else if ([type isEqualToString:@"medicine"]){
        //用药管理
        LGMedicineManageController *controller = [[LGMedicineManageController alloc]init];
        controller.patientid = self.patientid;
        [self.navigationController pushViewController:controller animated:YES];
        
    }else if ([type isEqualToString:@"monitor"]){
        //体征监测
        LGHomeMonitoringController *controller = [[LGHomeMonitoringController alloc]init];
        controller.diseaseCode = self.diseaseCode;
        controller.patientid = self.patientid;
        [self.navigationController pushViewController:controller animated:YES];
    }
}

//底部按钮
-(void)bottomButtonTouchedWithIndex:(NSInteger)index{
    if (index == 1) {
        //调整随访计划
        LGPatientCarePlanController *controller = [[LGPatientCarePlanController alloc]init];
        controller.diseaseCode = self.diseaseCode;
        controller.patientid = self.patientid;
        [self.navigationController pushViewController:controller animated:YES];
        
    }else if (index == 2){
        //开具医嘱
        LGAddDoctorAdviceController *controller = [[LGAddDoctorAdviceController alloc]init];
        controller.patientid = self.patientid;
        controller.followid = @"";
        [self.navigationController pushViewController:controller animated:YES];
        
    }else if (index == 3){
        //添加随访总结
        LGMedicineBallFollowUpRecorder *controller = [[LGMedicineBallFollowUpRecorder alloc]init];
        controller.planid = @"0";
        controller.patientid = self.patientid;
        [self.navigationController pushViewController:controller animated:YES];
    }
}

//风险监测
-(void)editRiskAction{
    LGPatientRiskMonitorController *controller = [[LGPatientRiskMonitorController alloc]init];
    controller.patientid = self.patientid;
    controller.canEdit = self.canEdit;
    [self.navigationController pushViewController:controller animated:YES];
}

//编辑基本信息
-(void)editBasicInfoAction:(NSString *)urlStr{
    LGGetWebViewController *controller = [[LGGetWebViewController alloc]init];
    controller.urlStr = urlStr;
    [self.navigationController pushViewController:controller animated:YES];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *tempArray = self.dataArray[section][@"items"];
    return tempArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGBasicInfoCommonCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *tempDic = self.dataArray[indexPath.section][@"items"][indexPath.row];
    NSString *key = LGNSString(tempDic[@"key"]);
    NSString *superKey = LGNSString(tempDic[@"superKey"]);
    cell.title = LGNSString(tempDic[@"title"]);
    NSString *content = @"";
    NSString *subKey = [tempDic.allKeys containsObject:@"subKey"]?tempDic[@"subKey"]:@"";
    NSString *statusKey = [tempDic.allKeys containsObject:@"status"]?tempDic[@"status"]:@"";
    NSString *status = @"";
    NSString *statusContent = @"";
    if ([self.dataDic isKindOfClass:[NSDictionary class]] && [self.dataDic.allKeys containsObject:superKey]) {
        NSDictionary *superDic = self.dataDic[superKey];
        if ([superDic isKindOfClass:[NSDictionary class]] && [superDic.allKeys containsObject:key]) {
            if ([superDic[key] isKindOfClass:[NSArray class]]) {
                //内容是个数组
                NSArray *itemArray = superDic[key];
                if ([key isEqualToString:@"shouss_fa"]) {
                    //手术史是个对象数组
                    for (NSDictionary *contentDic in itemArray) {
                        content = (content.length>0)?[NSString stringWithFormat:@"%@、%@ %@",content,contentDic[@"title"],contentDic[@"date"]]:[NSString stringWithFormat:@"%@ %@",contentDic[@"title"],contentDic[@"date"]];
                    }
                }else{
                    //字符串数组
                    for (NSString *contentStr in itemArray) {
                        content = (content.length>0)?[NSString stringWithFormat:@"%@、%@",content,contentStr]:contentStr;
                    }
                }
                
            }else{
                //内容是字符串
                content = LGNSString(superDic[key]);
            }
            
            //有状态
            if (subKey.length>0) {
                status = LGNSString(superDic[statusKey]);
                statusContent = LGNSString(superDic[subKey]);
            }
        }
        if ([tempDic.allKeys containsObject:@"color"] && status.length>0) {
            NSDictionary *colorDic = tempDic[@"color"];
            cell.statusLabel.textColor = [UIColor colorWithString:colorDic[status]];
        }else{
            cell.statusLabel.textColor = [UIColor clearColor];
        }
        cell.status = statusContent;
        cell.content = content;
        cell.showTip = NO;
        cell.vipLevel = @"";
        NSArray *tempArray = self.dataArray[indexPath.section][@"items"];
        if (indexPath.row == tempArray.count-1){
            cell.type = LGPositionTypeBottom;
        }else{
            cell.type = LGPositionTypeMiddle;
        }
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([[NSObject appType:self.diseaseCode] isEqualToString:@"XLZS"] && indexPath.section == 0) {
        //风险监测
        [self editRiskAction];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return viewPix(45);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    LGBaseInfoHeaderView *headView = [[LGBaseInfoHeaderView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(45))];
    headView.editButton.hidden = (self.canEdit == YES)?NO:YES;
    NSDictionary *tempDic = self.dataArray[section];
    NSString *timeKey = LGNSString(tempDic[@"timeKey"]);
    NSString *urlKey = LGNSString(tempDic[@"editKey"]);
    NSString *superKey = LGNSString(tempDic[@"key"]);
    NSDictionary *superDic = self.dataDic[superKey];
    headView.titleLabel.text = tempDic[@"title"];
    headView.dateLabel.text = [NSString stringWithFormat:@"%@",superDic[timeKey]];
    headView.urlStr = LGNSString(superDic[urlKey]);
    if ([[NSObject appType:self.diseaseCode] isEqualToString:@"XLZS"] && section == 0) {
        [headView.editButton addTarget:self action:@selector(editRiskAction) forControlEvents:UIControlEventTouchUpInside];
    }else{
        headView.delegate = self;
    }
    return headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return viewPix(8);
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(8))];
    footView.backgroundColor = [UIColor clearColor];
    return footView;
}


-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight-viewPix(65)-bottomSafeBarHeight/4.0) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGBasicInfoCommonCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = self.headView;
    }
    return _tableView;
}
-(LGNormalClinicHeader *)headView{
    if(!_headView){
        _headView = [[LGNormalClinicHeader alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(217))];
        _headView.delegate = self;
    }
    return _headView;
}

-(LGBottomButtonView *)bottomView{
    if(!_bottomView){
        _bottomView = [[LGBottomButtonView alloc] initWithItemArray:@[@{@"title":@"调整随访计划",@"color":@"#5E8EF5",@"type":@"bolder"},@{@"title":@"开具医嘱",@"color":@"#5E8EF5",@"type":@"bolder"},@{@"title":@"添加随访总结",@"color":@"#5E8EF5",@"type":@"bolder"}] frame:CGRectMake(0, Screen_H-topBarHeight-viewPix(65)-bottomSafeBarHeight/4.0, Screen_W, viewPix(65)+bottomSafeBarHeight/4.0)];
        _bottomView.delegate = self;
    }
    return _bottomView;
}

@end
